package com.sdry.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.sdry.model.zc.ZcSysUserEntity;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseUtils {

    public static final Integer AFFECT_ROW = 0;

    /**
     * 获取session
     */
    public static Long getSession() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Long wareHouseId = (Long)request.getSession().getAttribute("warehouse_id")==null?0: (Long) request.getSession().getAttribute("warehouse_id");
        return wareHouseId;
    }
    
    /**
     * 获取session中的用户信息
     */
    public static ZcSysUserEntity getUserBySession() {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        ZcSysUserEntity user = (ZcSysUserEntity)request.getSession().getAttribute("user");
        return user;
    }

    public static Long getSessionWareHouseId(HttpSession httpSession) {
        Long warehouse_id = (Long) httpSession.getAttribute("warehouse_id");
        return warehouse_id == null?0:warehouse_id;
    }



    public static Map<String,Object> getDateTableResult(List<?> materielList, Integer count) {
       /* JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(materielList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        return jsonObject;*/

        Map<String,Object> map = new HashMap<>();
        //JSONArray jsonArray = JSONArray.fromObject(materielList);
        map.put("code", AFFECT_ROW);
        map.put("data", materielList);
        map.put("count", count);
        return map;
    }

    /**
     * 产生三位随机数
     */
    public static String  getRandom() {
        String number=String.valueOf((int)(Math.random()*9+1));

        for(int i=0;i<=1;) {
            String newnumber = String.valueOf((int) (Math.random() * 9));
            if (!number.contains(newnumber)) {
                number = number + newnumber;
                i++;
            }
        }
        return "-"+number;
    }

}
